import { createRouter, createWebHistory } from 'vue-router'
import HomeView from '../views/HomeView.vue'
import store from "@/store";

const routes = [
  {
    path: '/',
    name: 'app',
    redirect: '/login'
  },
  {
    path: '/login',
    name: 'login',
    component:()=>import('../views/login.vue')
  },
  {
    path: '/about',
    name: 'about',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/AboutView.vue')
  },
  {
    path: '/menu',
    name:'/menu',
    component:()=>import('../views/menu.vue'),
    children:[{
      path:'/question',
      name:'question',
      component:()=>import('../views/QuestionTable.vue')
    },
    ]
  },
  {
    path:'/question',
    name:'question',
    component:()=>import('../views/QuestionTable.vue')
  },
  {
    path:'/questionInfo/:id',
    name:'questionInfo',
    component:()=>import('../views/QuestionInfo.vue')
  },
  {
    path: '/manager/questionInfo/:id',
    name:'managerQuestionInfo',
    component:()=>import('../views/ManagerQuestionInfo')
  },
  {
    path: '/question/manager',
    name:'/questionManager',
    component:()=>import('../views/QuestionManager')
  },
  {
    path: '/type/manager',
    name:'/type/Manager',
    component:()=>import('../views/TypeManager')
  },
  {
    path:'/menu',
    name:'/menu',
    component:()=>import('../views/menu')
  }
]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})

export default router
// 路由登录拦截
router.beforeEach((to, from, next) => {
  // 要不要对meta.loginRequire属性做监控拦截
  if (to.matched.some(function (item) {
    console.log(item, "是否需要登录校验：", item.meta.loginRequire || false);
    return item.meta.loginRequire
  })) {
    const _member = JSON.parse(store.state.userInfo);
    console.log("页面登录校验开始：", _member.token);
    if (!_member.token) {
      console.log("用户未登录或登录超时！");
      next('/login');
    } else {
      next();
    }
  } else {
    next();
  }
});